package com.fypg.module.medical.dal.mysql.hospitalmap;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.fypg.framework.mybatis.core.mapper.BaseMapperX;
import com.fypg.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.fypg.module.medical.controller.app.map.vo.AppHospitalMapReqVo;
import com.fypg.module.medical.dal.dataobject.department.DepartmentDO;
import com.fypg.module.medical.dal.dataobject.hospital.HospitalDO;
import com.fypg.module.medical.dal.dataobject.hospitalmap.HospitalMapDO;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * 医院地图 Mapper
 *
 * @author PJY
 */
@Mapper
public interface HospitalMapMapper extends BaseMapperX<HospitalMapDO> {

    /**
     * 无搜索则返回所有医院
     *
     * @param reqVO 请求参数
     * @return 医院地图 DO 列表
     */
    default List<HospitalMapDO> selectListByConditions(AppHospitalMapReqVo reqVO) {
        return selectList(new LambdaQueryWrapperX<HospitalMapDO>()
            .eq(HospitalMapDO::getEnabledMark, 1)
            .eq(HospitalMapDO::getDeleted, 0)
            .like(StringUtils.isNotBlank(reqVO.getSearchContent()), HospitalMapDO::getName, reqVO.getSearchContent())
            .like(StringUtils.isNotBlank(reqVO.getProvince()), HospitalMapDO::getProvince, reqVO.getProvince())
            .like(StringUtils.isNotBlank(reqVO.getLevelType()), HospitalMapDO::getLevelType, reqVO.getLevelType())
            .like(StringUtils.isNotBlank(reqVO.getGradeType()), HospitalMapDO::getGradeType, reqVO.getGradeType())
        );
    }

    /**
     * 通过搜索科室名称来查找关联的医院列表（多表连接查询）
     *
     * @param reqVO 请求参数
     * @return 医院列表
     */
    default List<HospitalMapDO> selectListByDepartmentSearch(AppHospitalMapReqVo reqVO) {
        MPJLambdaWrapper<HospitalMapDO> wrapper = new MPJLambdaWrapper<HospitalMapDO>()
            .selectAll(HospitalMapDO.class)
            .innerJoin(HospitalDO.class, HospitalDO::getEncode, HospitalMapDO::getEncode)
            .innerJoin(DepartmentDO.class, DepartmentDO::getHospitalId, HospitalDO::getHospitalId)
            .eq(HospitalMapDO::getEnabledMark, 1)
            .eq(HospitalMapDO::getDeleted, 0)
            .eq(HospitalDO::getEnabledMark, 1)
            .eq(HospitalDO::getDeleted, 0)
            .like(StringUtils.isNotBlank(reqVO.getSearchContent()), DepartmentDO::getDepartmentName, reqVO.getSearchContent())
            .like(StringUtils.isNotBlank(reqVO.getProvince()), HospitalMapDO::getProvince, reqVO.getProvince())
            .like(StringUtils.isNotBlank(reqVO.getLevelType()), HospitalMapDO::getLevelType, reqVO.getLevelType())
            .like(StringUtils.isNotBlank(reqVO.getGradeType()), HospitalMapDO::getGradeType, reqVO.getGradeType());

        return this.selectJoinList(HospitalMapDO.class, wrapper);
    }
}
